package ywpt.components.acegi.service.realm;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;


import ywpt.components.acegi.domain.User;
import ywpt.service.system.UserService;

public class ShiroDbRealm extends AuthorizingRealm {

	
	private UserService userService;
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO �Զ���ɷ������
		System.out.println("into doGetAuthorizationInfo!!");
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
		UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
		System.out.println("token.getUsername()=" +token.getUsername());
		User user = userService.loadUserByUsername(token.getUsername());
		AuthenticationInfo authcInfo = null;
		     if (user == null) {
		    	 System.out.println("user == null !!!!");
		         throw new UnknownAccountException();// δ֪�˻�
		     } else if (user.isEnabled() == false) {
		    	 System.out.println("user.isEnabled() == false");
		         throw new LockedAccountException();// �˻����������Աjϵ
		     } else {
		    	 System.out.println("loadUserByUsername success");
		         authcInfo = new SimpleAuthenticationInfo(user.getUsername(),
		                                                  user.getPassword(),
		                                                  getName());
		         this.setSession("currentUser", user);
		     }
		        return authcInfo;
	}
	
	private void setSession(Object key, Object value) {
	     Subject currentUser = SecurityUtils.getSubject();
	     if (null != currentUser) {
	    	 System.out.println("setSession: null != currentUser");
	         Session session = currentUser.getSession(true);
	         if (null != session) {
	        	 System.out.println("currentUser.getSession() :null != session");
	             session.setAttribute(key, value);
	         } else
	        	 System.out.println("currentUser.getSession() :null = session");
	     } else
	    	 System.out.println("setSession: null = currentUser");
	}
	/**
	 * @return userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService Ҫ���õ� userService
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
